package com.one.square.oauth.controller;

import com.one.square.core.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

/**
 * @author cwm
 * @Description
 * @date 2021/10/28 上午10:51
 * @Version 1.0
 */
@Api(tags = "第三方客户端")
@RestController
@RequestMapping("/client")
@AllArgsConstructor
@Slf4j
public class ClientController {
    private final JdbcClientDetailsService jdbcClientDetailsService;

    @GetMapping("/list")
    @ApiOperation(value = "客户端应用列表", notes = "客户端应用列表")
    public Result<List<ClientDetails>> findList() {
        return Result.succeed(jdbcClientDetailsService.listClientDetails());
    }

    @GetMapping("/findById")
    @ApiOperation(value = "根据客户端id查询客户端信息", notes = "根据客户端id查询客户端信息")
    public Result<ClientDetails> loadClientByClientId(
            @RequestParam(value = "clientId")
            @ApiParam(name = "clientId", value = "客户端id", required = true)
                    String clientId) {
        return Result.succeed(jdbcClientDetailsService.loadClientByClientId(clientId));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加第三方客户端信息", notes = "添加第三方客户端信息")
    public Result add(@RequestBody BaseClientDetails BaseClientDetails) {
        jdbcClientDetailsService.addClientDetails(BaseClientDetails);
        return Result.succeed("添加成功");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除客户端信息", notes = "删除客户端信息")
    public Result delete(@RequestParam(value = "clientId")
                         @ApiParam(name = "clientId", value = "客户端id", required = true)
                                 String clientId) {
        jdbcClientDetailsService.removeClientDetails(clientId);
        return Result.succeed("删除成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新客户端信息", notes = "更新客户端信息")
    public Result update(@RequestBody BaseClientDetails BaseClientDetails) {
        jdbcClientDetailsService.updateClientDetails(BaseClientDetails);
        return Result.succeed("更新成功");
    }
}
